<template>
	<view>
		<uni-nav-bar v-if="userInfo && userInfo.vip"
			statusBar="true"
			shadow="true"
			backgroundColor="#e8b768"
			color="#fff"
			leftIcon="back"
			@clickLeft="backLastPage"
			title="食材分类">
		</uni-nav-bar>


		<uni-nav-bar statusBar="true"
			v-else
			shadow="true"
			backgroundColor="#ee7b2d"
			color="#fff"
			leftIcon="back"
			@clickLeft="backLastPage"
			title="食材分类">
		</uni-nav-bar>

		<view :class="['header',{'vip-header':userInfo.vip}]">
			<view class="nav-tabs">
				<button class="active"
					type="default">分类</button>
				<button type="default">食材</button>
			</view>

			<input type="text"
				value=""
				class="search-bar"
				placeholder="想吃什么搜这里, 如麻婆豆腐" />

		</view>


		<view class="content-container">
			<scroll-view scroll-y="true"
				enable-flex
				class="side-menu">

				<view @tap="handleTapSideItem"
					v-for="(item,index) in categories"
					:key="item._id"
					:class="['side-item',{active: sideIndex == index}]"
					:id="index">
					{{item.cat_name}}
				</view>
			</scroll-view>

			<scroll-view scroll-y="true"
				enable-flex
				class="main-menu"
				:scroll-into-view="'main'+sideIndex"
				scroll-with-animation>


				<view class="main-item"
					v-for="(item,index) in categories"
					:key="item.cat_id"
					:id="'main'+index">
					<view class="item-title">
						{{item.cat_name}}
					</view>
					<view class="item-content">
						<view class="cat-item"
							v-for="subItem in item.children"
							:key="subItem.cat_id">
							<image :src="subItem.cat_icon"
								mode=""></image>
							<text>{{subItem.cat_name}}</text>
						</view>




					</view>
				</view>
			</scroll-view>
		</view>



	</view>
</template>

<script>
	import backLastPage from '../../mixins/backLastPage.js'
	export default {
		data() {
			return {
				sideIndex: 0,
				categories: [],
				userInfo: null
			}
		},
		mixins: [backLastPage],
		onLoad() {
			this.getCategories();
			this.userInfo = getApp().globalData.userInfo;
		},

		methods: {
			async getCategories() {
				let { message: res } = await this.$api.mCategory.getCategories();
				this.categories = res;
			},
			handleTapSideItem(e) {
				this.sideIndex = e.currentTarget.id;
				// console.log(this.sideIndex);
			}
		}
	}
</script>

<style lang="scss"
	scoped>
	uni-nav-bar ::v-deep {
		position: relative;
		z-index: -1;
	}


	.header {
		position: relative;
		top: -5rpx;
		z-index: 1000;
		width: 750rpx;
		height: 190rpx;
		background-color: #ee7b2d;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-around;

		&.vip-header {
			background-color: #e8b768;
		}

		.nav-tabs {
			display: flex;
			align-items: center;
			width: 378rpx;
			height: 48rpx;


			button {
				position: relative;

				width: 50%;
				height: 48rpx;
				line-height: 43rpx;
				font-size: 28rpx;
				box-sizing: border-box;
				color: #fff;
				background-color: transparent;
				border: 4rpx solid #fff;
				border-radius: 48rpx;

				&:first-of-type {
					border-right: 0;
					left: 20rpx;
				}

				&:last-of-type {
					border-left: 0;
					right: 20rpx;
				}
			}

			.active {
				color: #ee7b2d;
				background-color: #fff;
				border-radius: 48rpx;
				z-index: 1000;

			}
		}

		.search-bar {
			width: 692rpx;
			height: 60rpx;
			background-color: #f1f1f1;
			border-radius: 16rpx;
			background-image: url(../../static/img/caipusousuo_slices/mengbanzu%202.png);
			background-repeat: no-repeat;
			background-position: 20rpx 15rpx;
			text-align: center;
			font-size: 28rpx;
		}
	}

	.content-container {
		display: flex;
		justify-content: space-between;
		align-items: center;
		position: relative;
		top: -5rpx;
		width: 100vw;
		height: calc(100vh - 370rpx);
		background-color: #fff;

		.side-menu {
			display: flex;
			flex-direction: column;
			align-items: center;
			width: 180rpx;
			height: 100%;
			background-color: #fff;

			.side-item {
				width: 100%;
				height: 184rpx;
				line-height: 184rpx;
				text-align: center;
				font-size: 28rpx;

				&.active {
					background-color: #f6f6f6;
					color: #f09c60;
				}
			}

		}

		.main-menu {
			width: calc(750rpx - 180rpx);
			height: 100%;
			background-color: #f3f3f3;

			.main-item {
				width: 100%;
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-bottom: 20rpx;

				.item-title {
					width: 100%;
					height: 58rpx;
					font-size: 28rpx;
					font-weight: bold;
					box-sizing: border-box;
					color: #C0C4CC;
					padding: 10rpx 26rpx 10rpx 26rpx;
					margin-bottom: 10rpx;
				}

				.item-content {
					width: 100%;
					display: flex;
					flex-wrap: wrap;
					align-items: center;

					.cat-item {
						display: flex;
						flex-direction: column;
						align-items: center;
						width: max-content;
						padding: 15rpx 45rpx;

						image {
							width: 100rpx;
							height: 100rpx;
							margin-bottom: 12rpx;
							border-radius: $uni-border-radius-lg;
						}

						text {
							text-align: center;
							font-size: 18rpx;
							color: #989898;
						}
					}

				}
			}
		}
	}
</style>
